<?php
$this->load->view('header');

if (strlen($this->session->userdata('Error')) > 0) {
    echo '<div class="alert alert-danger alert-dismissable">';
    echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . $this->session->userdata('Error') . '</div>';
    $this->session->unset_userdata('Error');
} elseif (strlen($this->session->userdata('Success')) > 0) {
    echo '<div class="alert alert-success alert-dismissable">';
    echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . $this->session->userdata('Success') . '</div>';
    $this->session->unset_userdata('Success');
}
?>

<script type="text/javascript">
    function search_data() {
        $.post("<?= site_url() ?>setup/search_warehouse", {
            search_by_name: $('#search_by_name').val(),
            search_by_status: $('#search_by_status').val() 
        },
        function(data) {
            $('#tablecontent').html(data);
        });
    }

    function load_page_warehouse(page, ipp) {
        $.post("<?= site_url() ?>setup/load_page_warehouse", {
            page: page,
            ipp: ipp,
            search_by_name: $('#search_by_name').val(),
            search_by_status: $('#search_by_status').val()
        },
        function(data) {
            $('#tablecontent').html(data);
        });
    }

    function activation_item(warehouse_id, cond) {
        var page = $('#page_val').val();
        var ipp = $('#ipp_val').val();
        var str = 'Are you sure to deactivate this item?';
        if(cond!='Y'){
            str = 'Are you sure to activate this item?';
        }
        var cek = confirm(str);
        if (cek) {
            $.post("<?= site_url() ?>setup/activation_item", {
                warehouse_id: warehouse_id,
                cond : cond
            },
            function(data) {
                load_page_warehouse(page, ipp);				
            });
        }
    }

    function view_detail(warehouse_id,type){
		$.post("<?= site_url() ?>setup/view_detail", {warehouse_id: warehouse_id, type: type}, function(data) {
            $('#loader').html(data);
        });
	}
	
	function add_data() {
        $.post("<?= site_url() ?>setup/add_item_view", function(data) {
            $('#loader').html(data);
        });
    }
	
	function edit_data(warehouse_id) {		
        $.post("<?= site_url() ?>setup/edit_item_view", {warehouse_id: warehouse_id}, function(data) {
            $('#loader').html(data);
        });
    }
	
	function delete_data(warehouse_id){		
		$.post("<?= site_url() ?>setup/delete_item", {warehouse_id: warehouse_id}, function(data) {
            $('#loader').html(data);
        });
	}
	
	function view_borrow() {
        $.post("<?= site_url() ?>setup/item_borrow", function(data) {
            // $('#loader').html(data);
			window.location = '<?php echo base_url();?>setup/item_borrow';
        });
    }
</script>

<div id="loader" class="container">
    <form method=post id="access" action="#" enctype="multipart/form-data" class="form-horizontal" role="form">
	<div class="col-md-offset-1">
		<div class="col-md-11">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h4 class="panel-title"><b class="caret"></b><b> Search Warehouse Item</b></h4>
				</div>
				<div class="panel-body">
					<div class="col-md-6">
						<div class="form-group">
							<label class="col-md-4"><p class="text-left">Item Name</p></label>
							<div class="col-md-7"><input type="text" class="form-control input-sm" id="search_by_name" name="search_by_name"></div>
						</div>						
					</div>
					<div class="col-sm-6 col-md-5">
						<div class="form-group">
							<label class="col-md-4"><p class="text-left">Item Status</p></label>
							<div class="col-md-8">
								<select name="search_by_status" id="search_by_status" class="form-control input-sm">                         
									<option value="">-- select --</option>			
									<option value="Y">Active</option>
									<option value="N">Not Active</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<div class="col-md-4"><label for="button" class="control-label">&nbsp;</label></div>
							<div class="col-md-4">
								<button type="button" class="btn btn-primary btn-block" onclick="search_data();">Search</button>
							</div>
							<div class="col-md-4">
								<button type="reset" class="btn btn-default btn-block">Clear</button>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="panel-body well">
				<div class="col-md-6">
					<div class="form-group">
						<div class="col-md-4">
							<button type="button" class="btn btn-success btn-block" onclick="add_data();">+ Add Room</button>
						</div>
						<div class="col-md-5">
							<button type="button" class="btn btn-success btn-block" onclick="view_borrow();">
							<!--span class="glyphicon glyphicon-edit"></span--> View Borrow</button>
						</div>
					</div>
				</div>
				<div id="tablecontent" class="col-md-12">						
					<div class="form-group">
						<div class="table-responsive">
							<table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
								<thead>
									<tr>
										<th class="tblhead" style="width:4%;text-align:center;">No</th>
										<th class="tblhead" style="width:13%;text-align:center;">Item Name</th>
										<th class="tblhead" style="width:11%;text-align:center;">Total Qty</th>
										<th class="tblhead" style="width:11%;text-align:center;">Qty Active</th>
										<th class="tblhead" style="width:11%;text-align:center;">Qty Not Active</th>
										<th class="tblhead" style="width:11%;text-align:center;">Qty Stand By</th>
										<th class="tblhead" style="width:11%;text-align:center;">Qty Borrow</th>
										<th class="tblhead" style="width:14%;text-align:center;">Status</th>
										<th class="tblhead" style="width:14%;text-align:center;">Action</th>
									</tr>
								</thead>
								<tbody>
									<?php
									if (empty($list_data)) {
										?>
										<tr><td colspan="9" style="text-align:center">No Data Available</td></tr>
										<?php
									} else {
										foreach ($list_data as $row) {
											?>
											<tr>
												<?php
												$warehouse_id 		 		= $row["WAREHOUSE_ID"];
												$warehouse_item_name 		= $row["WAREHOUSE_ITEM_NAME"];
												$warehouse_item_qty 		= $row["WAREHOUSE_ITEM_QTY"];
												$warehouse_item_status 		= $row["WAREHOUSE_ITEM_STATUS"];
												$warehouse_item_qty_in 		= $row["WAREHOUSE_ITEM_QTY_IN"];
												$warehouse_item_qty_out		= $row["WAREHOUSE_ITEM_QTY_OUT"];
												$warehouse_item_active		= $row["WAREHOUSE_ITEM_ACTIVE"];
												$warehouse_item_not_active	= $row["WAREHOUSE_ITEM_NOT_ACTIVE"];
												$warehouse_status 	= ($warehouse_item_status == 'Y') ? 'Active' : 'Not Active';
												?>

												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;"><?php echo $row["RECNUM"]; ?>.</td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_name; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_qty; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_active; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_not_active; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_qty_in; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $warehouse_item_qty_out; ?></td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;">
													<?php if($warehouse_item_status=='Y') :?>
														<a class="btn btn-sm btn-primary" href="#" onclick="activation_item('<?php echo $warehouse_id ?>', '<?php echo $warehouse_item_status ?>');">
														<!--span class="glyphicon glyphicon-ok"></span--> <?php echo $warehouse_status; ?></a>
													<?php else: ?>
														<a class="btn btn-sm btn-danger" href="#" onclick="activation_item('<?php echo $warehouse_id ?>', '<?php echo $warehouse_item_status ?>');">
														<!--span class="glyphicon glyphicon-ban-circle"></span--> <?php echo $warehouse_status; ?></a>
													<?php endif; ?>
												</td>
												<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;">
													<a class="btn btn-sm btn-warning" href="#" onclick="edit_data('<?php echo $warehouse_id ?>');">
														<!--span class="glyphicon glyphicon-edit"></span--> Edit</a>
													<a class="btn btn-sm btn-danger" href="#" onclick="delete_data('<?php echo $warehouse_id ?>');">
														<!--span class="glyphicon glyphicon-delete"></span--> Delete</a>
												</td>
											</tr>
											<?php
										}
									}
									?>
								</tbody>
							</table>
						</div>
					</div>
					<div class="form-group">
						<div id="mypaging">
							<?php
							echo $paging;
							echo $jump_page;
							echo $ipp;
							?>
						</div>								
					</div>
				</div>
			</div>
		</div>
	</div>
	</form>
</div>

<?php $this->load->view('footer'); ?>